package cn.keduox.controller;

import cn.keduox.entity.*;
import cn.keduox.service.impl.BookRecTypeServiceImpl;
import cn.keduox.service.impl.BookServiceImpl;
import cn.keduox.service.impl.HotBookTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * User: Yongman
 * Date: 2017/12/30
 */
@WebServlet("/book.do")
public class BookController extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        List<FirstType> firstTypes=findAllType(request,response);
//        request.setAttribute("firstTypeList",firstTypes);

//        调用推荐书目查询方法
        List<BookRecType> bookRecTypes = bookRecType();
        request.setAttribute("bookRecTypes", bookRecTypes);
//        调用畅销榜方法
        List<HotBookType> hotBookTypes = hotBookType();
        List<HotBook> hotBookList = hotBookTypes.get(0).getHotBookList();
        Book book = hotBookList.get(0).getBook();
        request.setAttribute("hotBookTypes", hotBookTypes);

        request.getRequestDispatcher("/WEB-INF/pages/book_rec.jsp").forward(request,response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    private List<FirstType> findAllType(HttpServletRequest request, HttpServletResponse response) {
        BookServiceImpl bookService=new BookServiceImpl();
        List<FirstType> firstTypes=bookService.findAllType();
        return firstTypes;
    }

    /**
     * 图书推荐部分，包括主编推荐、本周精选、新书速递、读者热评
     */
    public List<BookRecType> bookRecType(){
        BookRecTypeServiceImpl bookRecTypeService = new BookRecTypeServiceImpl();
        return bookRecTypeService.query();
    }

    /**
     * 畅销榜部分
     */
    public List<HotBookType> hotBookType(){
        HotBookTypeServiceImpl hotBookTypeService = new HotBookTypeServiceImpl();
        return hotBookTypeService.query();
    }
}
